#include <polarisdata/PolarisData.h>
#include "string.h"
#include "jsonrpc/icore.h"
#include <threadunit/TestSram.h>

rt_uint8_t PolarisData::buf[1024] = {0};
unsigned long PolarisData::len = 0;
unsigned long PolarisData::recvLen = 0;
int PolarisData::lastState = TCPSERVER_EVENT_CONNECT;
SdramData PolarisData::sdramData;

extern rt_mailbox_t polaris_data_mail;
//static unsigned int k = 0;
static unsigned int error = 0;

/**
 * @brief Read data from SDRAM, transfer it to FSMC, and verify the validity of the data
 * （从SDRAM读取数据，并将这些数据传输到FSMC，并验证数据的有效性）
 * @param parameter unused
 */
void PolarisData_entry(void *parameter)
{
    PolarisData polarisData;
    Icore::getInstance().sram->fsmc->resetSram();
    while (1)
    {
        if (error == 1)
            while (1)
            {
                rt_thread_mdelay(10);
            }

        rt_thread_mdelay(10);

        uint32_t num = polarisData.sdramData.ReadyToFsmc();
        if (num != 0)
        {
            rt_kprintf("num:%d,rdAddr:%x\r\n", num, polarisData.sdramData.getRdAddr());
            Icore::getInstance().sram->fsmc->transData2Fsmc(num, polarisData.sdramData.getRdAddr());
            polarisData.sdramData.rdAddrCount(num / 2);

            rt_thread_mdelay(5);

            for (uint32_t i = 0; i < num / 2; i++)
            {
                unsigned short int data;
                // Icore::getInstance().sram->fsmc->readFsmc(500, &data);
                // rt_kprintf("data %x:%x\r\n",i,data);

                Icore::getInstance().sram->fsmc->readFsmc(501, &data);
                break;
                rt_thread_mdelay(1000);
                //                if(k > 0xff) k = 0;
                //                if((data%0x100) != k) {rt_kprintf("error data 1 %d\r\n",data);error=1;break;}
                //                k++;
                //                if((data/0x100) != k) {rt_kprintf("error data 2 %d\r\n",data);error=1;break;}
                //                k++;
            }
        }
    }
}

int PolarisData_init(void)
{
    rt_thread_t tid1 = RT_NULL;
    tid1 = rt_thread_create("PolarisData", PolarisData_entry, NULL, 2048, 15, 10);
    if (tid1 != RT_NULL)
    {
        rt_thread_startup(tid1);
    }

    return 1;
}
// INIT_APP_EXPORT(PolarisData_init);

PolarisData::PolarisData()
{
    comm = tcpserver_create(RT_LWIP_IPADDR, port);
    tcpserver_set_notify_callback(comm, commNotify);
    //    rt_kprintf("PolarisData::buf addr:%d\r\n",(unsigned int)buf);
}

PolarisData::~PolarisData()
{
    // TODO 自动生成的析构函数存根
}

void PolarisData::commNotify(tcpclient_t client, rt_uint8_t event)
{
    int ret;
    switch (event)
    {
    case TCPSERVER_EVENT_CONNECT:
        // rt_kprintf("tcpserver connect:%d\r\n", client->sock);
        break;
    case TCPSERVER_EVENT_DISCONNECT:
        // rt_kprintf("client disconnect:%d\r\n", client->sock);
        break;
    case TCPSERVER_EVENT_RECV:
        ret = tcpserver_recv(client, buf, 1024, -1);
        if (ret > 0)
        {
            // 传入数据必须为16位的整数倍
            if (lastState == TCPSERVER_EVENT_CONNECT)
            {
                len = buf[0] + buf[1] * 0x100 + buf[2] * 0x10000 + buf[3] * 0x1000000;
                recvLen = ret - 4;
                sdramData.dma_config((unsigned int)(buf + 4), ret - 4);
                rt_kprintf("TCPSERVER_EVENT_RECV:%d\r\n", len);
            }
            else
            {
                recvLen += ret;
                while (sdramData.isBusy())
                    rt_thread_delay(1);
                sdramData.dma_config((unsigned int)buf, ret);
            }

            if (recvLen == len)
            {
                const char *finish = "DATA ALL RECEIVE!";
                tcpserver_send(client, (void *)finish, strlen(finish), 0);
                len = 0;
                recvLen = 0;
            }
        }

        break;
    default:
        rt_kprintf("unknown failed!\r\n");
        break;
    }

    lastState = event;
}
